home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / mweb / MWEB Utils / ws295sdk.exe / Ws2sdkzp.exe / SAMPLES / LAYERED / INST_LSP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-06  |  14.7 KB  |  414 lines

  1. /*++
  2.  
  3.      Copyright (c) 1996 Intel Corporation
  4.      Copyright (c) 1996 Microsoft Corporation
  5.      All Rights Reserved
  6.  
  7.      Permission is granted to use, copy and distribute this software and
  8.      its documentation for any purpose and without fee, provided, that
  9.      the above copyright notice and this statement appear in all copies.
  10.      Intel makes no representations about the suitability of this
  11.      software for any purpose.  This software is provided "AS IS."
  12.  
  13.      Intel specifically disclaims all warranties, express or implied,
  14.      and all liability, including consequential and other indirect
  15.      damages, for the use of this software, including liability for
  16.      infringement of any proprietary rights, and including the
  17.      warranties of merchantability and fitness for a particular purpose.
  18.      Intel does not assume any responsibility for any errors which may
  19.      appear in this software nor any responsibility to update it.
  20.  
  21.  
  22. Module Name:
  23.  
  24.     inst_lsp.cpp
  25.  
  26. Abstract:
  27.  
  28.     This module contains the installation routines for the winsock2 layered
  29.     service provider sample.
  30.  
  31. --*/
  32.  
  33. #define WIN32_LEAN_AND_MEAN
  34.  
  35. #include "warnoff.h"
  36. #include <winsock2.h>
  37.  
  38. #include "ws2spi.h"
  39. #include <rpc.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include "sporder.h"
  43. #include "install.h"
  44. #define LAYERED_PROVIDER_NAME L"LAYERED_PROVIDER"
  45.  
  46. VOID
  47. UninstallMyProvider()
  48. {
  49.     INT Errno;
  50.  
  51.     WSCDeinstallProvider(
  52.         &LayeredProviderGuid,
  53.         &Errno);
  54. }
  55.  
  56. INT
  57. InstallMyProvider(
  58.     PDWORD CatalogId
  59.     )
  60. /*++
  61. --*/
  62. {
  63.     WSAPROTOCOL_INFOW  proto_info;
  64.     int               install_result;
  65.     int               install_error;
  66.  
  67.     // Create a PROTOCOL_INFO to install for our provider DLL.
  68.     proto_info.dwServiceFlags1 = 0;
  69.     proto_info.dwServiceFlags2 = 0;
  70.     proto_info.dwServiceFlags3 = 0;
  71.     proto_info.dwServiceFlags4 = 0;
  72.     proto_info.dwProviderFlags = PFL_HIDDEN;
  73.     proto_info.ProviderId      = LayeredProviderGuid;
  74.     proto_info.dwCatalogEntryId = 0;   // filled in by system
  75.     proto_info.ProtocolChain.ChainLen = LAYERED_PROTOCOL;
  76.         // Do  not  need  to  fill  in  chain  for LAYERED_PROTOCOL or
  77.         // BASE_PROTOCOL
  78.     proto_info.iVersion = 0;
  79.     proto_info.iAddressFamily = AF_INET;
  80.     proto_info.iMaxSockAddr = 16;
  81.     proto_info.iMinSockAddr = 16;
  82.     proto_info.iSocketType = SOCK_STREAM;
  83.     proto_info.iProtocol = IPPROTO_TCP;   // mimic TCP/IP
  84.     proto_info.iProtocolMaxOffset = 0;
  85.     proto_info.iNetworkByteOrder = BIGENDIAN;
  86.     proto_info.iSecurityScheme = SECURITY_PROTOCOL_NONE;
  87.     proto_info.dwMessageSize = 0;  // stream-oriented
  88.     proto_info.dwProviderReserved = 0;
  89.     wcscpy(
  90.         proto_info.szProtocol,
  91.         LAYERED_PROVIDER_NAME);
  92.  
  93.     install_result = WSCInstallProvider(
  94.         &LayeredProviderGuid,
  95.         L"lsp.dll",                   // lpszProviderDllPath
  96.         & proto_info,                 // lpProtocolInfoList
  97.         1,                            // dwNumberOfEntries
  98.         & install_error);             // lpErrno
  99.     *CatalogId = proto_info.dwCatalogEntryId;
  100.  
  101.     return(install_result);
  102.  
  103. } // Install_My_Layer
  104.  
  105. INT
  106. InstallNewChain(
  107.     LPWSAPROTOCOL_INFOW BaseProtocolInfoBuff,
  108.     DWORD               LayeredProviderCatalogId,
  109.     HKEY                ConfigRegisteryKey
  110.     )
  111. {
  112.     WSAPROTOCOL_INFOW ProtocolChainProtoInfo;
  113.     WCHAR             DebugPrefix[] = L"LAYERED ";
  114.     INT               ReturnCode;
  115.     INT               Errno;
  116.     UUID              NewChainId;
  117.     RPC_STATUS        Status;
  118.     PUCHAR            GuidString;
  119.     HKEY              NewKey;
  120.     DWORD             KeyDisposition;
  121.     BOOL              Continue;
  122.  
  123.     ReturnCode = NO_ERROR;
  124.  
  125.     // We are only layering on top of base providers
  126.  
  127.     if (BaseProtocolInfoBuff->ProtocolChain.ChainLen == BASE_PROTOCOL){
  128.         Continue = FALSE;
  129.  
  130.         // Get a new GUID for the protocol chain we are about to install
  131.         Status = UuidCreate(
  132.             &NewChainId);
  133.         if (RPC_S_OK == Status){
  134.  
  135.             //Get the string representaion of the GUID
  136.             Status = UuidToString(
  137.                 &NewChainId,
  138.                 &GuidString);
  139.             if (RPC_S_OK == Status){
  140.                 // Write down the GUID  in the registry so we know who to
  141.                 // uninstall
  142.                 RegCreateKeyEx(
  143.                     ConfigRegisteryKey,                 // hkey
  144.                     (LPCSTR)GuidString,                 // lpszSubKey
  145.                     0,                                  // dwReserved
  146.                     NULL,                               // lpszClass
  147.                     REG_OPTION_NON_VOLATILE,            // fdwOptions
  148.                     KEY_ALL_ACCESS,                     // samDesired
  149.                     NULL,                               // lpSecurityAttributes
  150.                     & NewKey,                           // phkResult
  151.                     & KeyDisposition                    // lpdwDisposition
  152.                     );
  153.                 RpcStringFree(&GuidString);
  154.  
  155.                 Continue =TRUE;
  156.             } //if
  157.             else{
  158.                 printf("UuidToString() Failed\n");
  159.             } //else
  160.         } //if
  161.         else{
  162.             printf("UuidCreate Failed\n");
  163.         } //else
  164.  
  165.         if (Continue){
  166.  
  167.             ProtocolChainProtoInfo = *BaseProtocolInfoBuff;
  168.  
  169.             ProtocolChainProtoInfo.ProviderId = NewChainId;
  170.  
  171.             wcscpy(
  172.                 ProtocolChainProtoInfo.szProtocol,
  173.                 DebugPrefix);
  174.             wcscat(
  175.                 ProtocolChainProtoInfo.szProtocol,
  176.                 BaseProtocolInfoBuff->szProtocol);
  177.  
  178.             ProtocolChainProtoInfo.ProtocolChain.ChainLen = 2;
  179.             ProtocolChainProtoInfo.ProtocolChain.ChainEntries[0] =
  180.                 LayeredProviderCatalogId;
  181.             ProtocolChainProtoInfo.ProtocolChain.ChainEntries[1] =
  182.                 BaseProtocolInfoBuff->dwCatalogEntryId;
  183.  
  184.             ReturnCode = WSCInstallProvider(
  185.                 &NewChainId,
  186.                 L"lsp.dll",
  187.                 &ProtocolChainProtoInfo,
  188.                 1,
  189.                 &Errno);
  190.             if (ReturnCode==0)
  191.                 printf ("Installed over %ls.\n",
  192.                      BaseProtocolInfoBuff->szProtocol);
  193.             else
  194.                 printf ("Installation over %ls failed with error %ld.\n",
  195.                      BaseProtocolInfoBuff->szProtocol, Errno);
  196.         } //if
  197.     } //if
  198.     return(ReturnCode);
  199. }
  200.  
  201. int
  202. main( int argc, char** argv)
  203. {
  204.     LPWSAPROTOCOL_INFOW   ProtocolInfoBuff = NULL;
  205.     DWORD                ProtocolInfoBuffSize = 0;
  206.     INT                  ErrorCode;
  207.     INT                  EnumResult;
  208.     LONG                 lresult;
  209.     HKEY                 NewKey;
  210.     DWORD                KeyDisposition;
  211.     GUID                 ProviderID;
  212.     INT                  Index;
  213.     DWORD                CatalogEntryId;
  214.     CHAR                 GuidStringBuffer[40];
  215.     DWORD                GuidStringBufferLen;
  216.     FILETIME             FileTime;
  217.     BOOL                 EntryIdFound;
  218.     DWORD                 *CatIdBuff;
  219.     DWORD                 nCatIds;
  220.  
  221.     // See if we are installing or deinstalling
  222.     lresult = RegOpenKeyEx(
  223.         HKEY_LOCAL_MACHINE,                     // hkey
  224.         CONFIGURATION_KEY,                      // lpszSubKey
  225.         0,                                      // dwReserved
  226.         KEY_ALL_ACCESS,                        // samDesired
  227.         & NewKey                               // phkResult
  228.         );
  229.  
  230.     if (ERROR_SUCCESS == lresult){
  231.         // The layered provider is installed so we are going uninstall.
  232.  
  233.         //
  234.         // Enumerate all the provider IDs we stored on install and deinstall
  235.         // the providers
  236.         //
  237.         printf("Removing Installed Layered Providers\n");
  238.  
  239.         Index = 0;
  240.         GuidStringBufferLen = sizeof(GuidStringBuffer);
  241.         lresult = RegEnumKeyEx(
  242.             NewKey,               //hKey
  243.             Index,                // Index of subkey
  244.             &GuidStringBuffer[0],    // Buffer to hold key name
  245.             &GuidStringBufferLen,  // Length of buffer
  246.             NULL,                 // Reserved
  247.             NULL,                 // Class buffer
  248.             NULL,                 // Class buffer length
  249.             &FileTime              // Last write time
  250.             );
  251.  
  252.         printf("Removing layered provider protocol chains\n");
  253.         while (lresult != ERROR_NO_MORE_ITEMS){
  254.             UuidFromString(
  255.                 (PUCHAR) GuidStringBuffer,
  256.                 &ProviderID);
  257.             // Deinstall the provider chain we installed
  258.             WSCDeinstallProvider(
  259.                 &ProviderID,
  260.                 &ErrorCode);
  261.             // Delete our registry key
  262.             RegDeleteKey(
  263.                 NewKey,
  264.                 &GuidStringBuffer[0]);
  265.  
  266.             GuidStringBufferLen = sizeof(GuidStringBuffer);
  267.         lresult = RegEnumKeyEx(
  268.             NewKey,               //hKey
  269.             Index,                // Index of subkey
  270.             &GuidStringBuffer[0],    // Buffer to hold key name
  271.             &GuidStringBufferLen,  // Length of buffer
  272.             NULL,                 // Reserved
  273.             NULL,                 // Class buffer
  274.             NULL,                 // Class buffer length
  275.             &FileTime              // Last write time
  276.             );
  277.  
  278.         } //while
  279.  
  280.         // Clen up the registry
  281.         RegCloseKey(
  282.             NewKey);
  283.         RegDeleteKey(
  284.             HKEY_LOCAL_MACHINE,
  285.             CONFIGURATION_KEY);
  286.  
  287.         // Uninstall the real provider
  288.         UninstallMyProvider();
  289.     } //if
  290.     else{
  291.  
  292.         RegCreateKeyEx(
  293.             HKEY_LOCAL_MACHINE,                 // hkey
  294.             CONFIGURATION_KEY,                  // lpszSubKey
  295.             0,                                  // dwReserved
  296.             NULL,                               // lpszClass
  297.             REG_OPTION_NON_VOLATILE,            // fdwOptions
  298.             KEY_ALL_ACCESS,                     // samDesired
  299.             NULL,                               // lpSecurityAttributes
  300.             & NewKey,                           // phkResult
  301.             & KeyDisposition                    // lpdwDisposition
  302.             );
  303.         // Install a dummy PROTOCOL_INFO for the layered provider.
  304.         lresult = InstallMyProvider(
  305.             &CatalogEntryId);
  306.         if (NO_ERROR == lresult){
  307.             //
  308.             // Enumerate the installed providers and chains
  309.             //
  310.             printf("Scanning Installed Providers\n");
  311.             // Call WSCEnumProtocols with a zero length buffer so we know what
  312.             // size to  send in to get all the installed PROTOCOL_INFO
  313.             // structs.
  314.             WSCEnumProtocols(
  315.                 NULL,                     // lpiProtocols
  316.                 ProtocolInfoBuff,         // lpProtocolBuffer
  317.                 & ProtocolInfoBuffSize,   // lpdwBufferLength
  318.                 & ErrorCode);             // lpErrno
  319.  
  320.             ProtocolInfoBuff = (LPWSAPROTOCOL_INFOW)
  321.                 malloc(ProtocolInfoBuffSize);
  322.             if (ProtocolInfoBuff){
  323.                 printf("Installing Layered Providers\n");
  324.  
  325.                 EnumResult = WSCEnumProtocols(
  326.                     NULL,                     // lpiProtocols
  327.                     ProtocolInfoBuff,         // lpProtocolBuffer
  328.                     & ProtocolInfoBuffSize,   // lpdwBufferLength
  329.                     & ErrorCode);
  330.  
  331.                 if (SOCKET_ERROR != EnumResult){
  332.  
  333.                     // Find our provider entry to get our catalog entry ID
  334.                     EntryIdFound = FALSE;
  335.                     for (Index =0; Index < EnumResult; Index++){
  336.                         if (memcmp (&ProtocolInfoBuff[Index].ProviderId,
  337.                                 &LayeredProviderGuid,
  338.                                 sizeof (LayeredProviderGuid))==0){
  339.  
  340.                             CatalogEntryId =
  341.                                 ProtocolInfoBuff[Index].dwCatalogEntryId;
  342.                             EntryIdFound = TRUE;
  343.                         } //if
  344.                     } //for
  345.                     if (EntryIdFound){
  346.                         for (Index =0; Index < EnumResult; Index++){
  347.                             InstallNewChain(
  348.                                 &ProtocolInfoBuff[Index],
  349.                                 CatalogEntryId,
  350.                                 NewKey);
  351.                         } //for
  352.                         free (ProtocolInfoBuff);
  353.  
  354.                         //
  355.                         // Enumerate the installed providers and chains
  356.                         //
  357.                         printf("Preparing To Reoder Installed Chains\n");
  358.                         // Call WSCEnumProtocols with a zero length buffer so we know what
  359.                         // size to  send in to get all the installed PROTOCOL_INFO
  360.                         // structs.
  361.                         ProtocolInfoBuffSize = 0;
  362.                         WSCEnumProtocols(
  363.                             NULL,                     // lpiProtocols
  364.                             NULL,                      // lpProtocolBuffer
  365.                             & ProtocolInfoBuffSize,   // lpdwBufferLength
  366.                             & ErrorCode);             // lpErrno
  367.  
  368.                         ProtocolInfoBuff = (LPWSAPROTOCOL_INFOW)
  369.                             malloc(ProtocolInfoBuffSize);
  370.                         if (ProtocolInfoBuff){
  371.                             printf("Reodering Installed Chains\n");
  372.  
  373.                             EnumResult = WSCEnumProtocols(
  374.                                 NULL,                     // lpiProtocols
  375.                                 ProtocolInfoBuff,         // lpProtocolBuffer
  376.                                 & ProtocolInfoBuffSize,   // lpdwBufferLength
  377.                                 & ErrorCode);
  378.  
  379.                             if (SOCKET_ERROR != EnumResult){
  380.                                 // Allocate buffer to hold catalog ID array
  381.                                 CatIdBuff = (DWORD *)
  382.                                     malloc (sizeof (DWORD)*EnumResult);
  383.                                 if (CatIdBuff!=NULL) {
  384.                                     // Put our catalog chains first
  385.                                     nCatIds = 0;
  386.                                     for (Index =0; Index < EnumResult; Index++){
  387.                                         if ((ProtocolInfoBuff[Index].ProtocolChain.ChainLen>1)
  388.                                                 && (ProtocolInfoBuff[Index].ProtocolChain.ChainEntries[0]==CatalogEntryId))
  389.                                             CatIdBuff[nCatIds++] = ProtocolInfoBuff[Index].dwCatalogEntryId;
  390.                                     }
  391.  
  392.                                     // Put the rest next
  393.                                     for (Index =0; Index < EnumResult; Index++){
  394.                                         if ((ProtocolInfoBuff[Index].ProtocolChain.ChainLen<=1)
  395.                                                 || (ProtocolInfoBuff[Index].ProtocolChain.ChainEntries[0]!=CatalogEntryId))
  396.                                             CatIdBuff[nCatIds++] = ProtocolInfoBuff[Index].dwCatalogEntryId;
  397.                                     }
  398.                                     // Save new protocol order
  399.                                     printf ("Saving New Protocol Order\n");
  400.                                     ErrorCode = WSCWriteProviderOrder (CatIdBuff, nCatIds);
  401.                                     if (ErrorCode!=NO_ERROR)
  402.                                         printf ("Reodering failed with error %ld", ErrorCode);
  403.                                 }
  404.                             }
  405.                         }
  406.                     } //if
  407.                 } //if
  408.             } //if
  409.         } //if
  410.     } //else
  411.     return(0);
  412. }
  413.  
  414.